package {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.getTimer;
	
	import pqstar.Node;
	import pqstar.PqAstar;
	
	[SWF(width=1920,height=900)]
	public class GameMap extends Sprite{
		private var _startX:int;
		private var _startY:int;
		private var _endX:int;
		private var _endY:int;
		private var _step:int;
		private var _astar:PqAstar;
		private var _canvas:BitmapData;
		private var _scene:Sprite;
		private var size:int=0;
		private var w:int=0;
		private var h:int=0;
		private var gw:int=0;
		private var gh:int=0;
		private var obsPanel:Sprite;
		private var obs:Array=[];
		public var bg:Sprite;
		public function GameMap():void{
			
		}
		/**
		 * 
		 * @param _obs 障碍点
		 * @param mapW 地图宽度
		 * @param mapH 地图高度
		 * @param _gridSize 格子大小
		 * @param _gridWidth 格子宽度
		 * @param _gridHeight 格子高度
		 * 
		 */		
		public function init(_obs:Array,mapW:int=1920,mapH:int=900,_gridRow:int=256,_gridColumn:int=256,_gridSize:int=5):void{
			obs=_obs;
			w=mapW;
			h=mapH;
			gw=_gridRow;
			gh=_gridColumn;
			size=_gridSize;
			_astar = new PqAstar();
			//_astar.isPrune = false;
			_canvas = new BitmapData(w,h);
			var bmp:Bitmap = new Bitmap(_canvas);
			obsPanel=new Sprite();
			_scene = new Sprite();
			bg=new Sprite();
			addChild(bmp);
			addChild(bg);
			addChild(_scene);
			addChild(obsPanel);
			//stage.addEventListener(MouseEvent.MOUSE_DOWN, onPress);
			onLink();
		}
		public function setMap(_map:DisplayObject):void{
			bg.addChild(_map);
		}
		
		private function onLink():void
		{
			var l:int;
			var i:int;
			var t:Node;
			_step = 0;
			_astar.createMap(obs,gw);
			//_scene.graphics.clear();
			//_canvas.fillRect(new Rectangle(0, 0, w, h), 0xFF00FF00)
			l = _astar.map.length;
			for (i = 0; i < l; i++)
			{
				t = _astar.map[i];
				if (t.block == _astar.mapVersion)
					_canvas.fillRect(new Rectangle(t.x * size, t.y * size, size, size), 0xFF000000);
			}
		}
		/**创建障碍点**/
		private function createObstacles():Array{
			var obs:Array=[];
			var obn:int=0;
			for(var z:int=0;z<gw;z++){
				obs[z]=[];
				for(var v:int=0;v<gh;v++){
					var ra:int=Math.random()*16;
					if(ra==2){
						obs[z][v]=true;
						obn++;
					}
				}
			}
			trace(obn);
			return obs;
		}
		public function findPath(startPoint:Point,endPoint:Point):Array{
			if (endPoint.x< 0 || endPoint.y >= w) return [];
			if (endPoint.x < 0 || endPoint.y >= h) return [];
			_startX = int(startPoint.x / size);
			_startY = int(startPoint.y / size);
			_endX = int(endPoint.x / size);
			_endY = int(endPoint.y / size);
			trace(startPoint.x+"------"+startPoint.y);
			trace(endPoint.x+"------"+endPoint.y);
			trace(_startX+"------"+_startY);
			trace(_endX+"------"+_endY);
			if (_astar.map[_startY << _astar.D1 | _startX].block == _astar.mapVersion) return [];
			if (_astar.map[_endY << _astar.D1 | _endX].block == _astar.mapVersion) return [];
			_astar.search(_startX, _startY, _endX, _endY);
			var l:int = _astar.path.length;
			var arr:Array=[];
			for(var i:int=0;i<l;i++){
				var t:Node=_astar.path[i];
				arr.push([t.x*size+size/2,t.y*size+size/2]);
			}
			trace(arr.toString());
			return arr.reverse();
		}
		private function onPress(e:MouseEvent):void
		{
			if (e.stageX < 0 || e.stageX >= w) return;
			if (e.stageY < 0 || e.stageY >= h) return;
			
			var graphics:Graphics = _scene.graphics;
			
			if (_step == 0)
			{
				graphics.clear();
				_startX = int(e.stageX / size);
				_startY = int(e.stageY / size);
				if (_astar.map[_startY << _astar.D1 | _startX].block == _astar.mapVersion) return;
				graphics.beginFill(0xFFFF0000);
				graphics.drawCircle(_startX * size + size/2, _startY * size + size/2, size/2);
				graphics.endFill();
				_step++;
			}
			else if (_step == 1)
			{
				_endX = int(e.stageX / size);
				_endY = int(e.stageY / size);
				if (_astar.map[_endY << _astar.D1 | _endX].block == _astar.mapVersion) return;
				graphics.beginFill(0xFFFF0000);
				graphics.drawCircle(_endX * size + size/2, _endY * size + size/2, size/2);
				graphics.endFill();
				
				var ttt:int = getTimer();
				_astar.search(_startX, _startY, _endX, _endY);
				var l:int = _astar.path.length;
				
				var t:Node = _astar.path[0];
				var i:int;
				graphics.lineStyle(1, 0x0000FF);
				for (i = 1; i < l; i++)
				{
					graphics.moveTo(t.x * size + size/2, t.y * size + size/2);
					t = _astar.path[i];
					graphics.lineTo(t.x * size + size/2, t.y * size + size/2);
				}
				_step = 0;
			}
		}
	}
	
}

